import 'package:flutter/material.dart';
import 'home/index.dart';
import 'contact/index.dart';
import 'discover/index.dart';
import 'mine/index.dart';
import '/utils/setting.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {

  int _selectedIndex = 2;
  static const List<Widget> _widgetList = <Widget>[
    HomePage(),
    ContactPage(),
    DiscoverPage(),
    MinePage(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  // 构建底部每一个tab项
  BottomNavigationBarItem _tabBarItem(String title, String imgUrl, String activeImgUrl) {
    return BottomNavigationBarItem(
      icon: Image.asset('assets/images/tab/$imgUrl.png', width: 28,),
      activeIcon: Image.asset('assets/images/tab/$activeImgUrl.png', width: 28,),
      label: title,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _widgetList[_selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          _tabBarItem('微信', 'nav_tab_1', 'nav_tab_1_on'),
          _tabBarItem('通讯录', 'nav_tab_2', 'nav_tab_2_on'),
          _tabBarItem('发现', 'nav_tab_3', 'nav_tab_3_on'),
          _tabBarItem('我的', 'nav_tab_4', 'nav_tab_4_on'),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        selectedItemColor: DefaultColor.success,
        unselectedItemColor: Colors.grey,
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.white,
        selectedFontSize: 12,
        unselectedFontSize: 12,
      ),
    );
  }
}
